package com.eshop.we.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.eshop.we.user.dto.UserBooksDto;
import com.eshop.we.user.dto.UserInfoDto;
import com.eshop.we.user.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @author anonymity
 * @since 2018-07-06
 */
@Mapper
@Component
public interface UserMapper extends BaseMapper<User> {


    @Select("select u.* ,s.name as schoolName from user u left join school s on s.id= u.school_id where u.id=#{userId}")
    UserBooksDto selectUserAndSchool(@Param("userId") Integer userId);

    @Update("update user set login_token =#{login_token} where open_id=#{open_id} ")
    Integer updateByOpenId(@Param("user") User user);

    @Select("<script>" +
            " select u.* ,s.name as schoolName, a.*  " +
            " from `user` u " +
            " left join account a on u.id= a.user_id " +
            " left join school s on s.id=u.school_id "+
            " where 1=1 "+
            " <if test='name!=null '>"+
            " and u.real_name like     CONCAT(CONCAT('%', #{name}), '%') "+
            " </if>"+
            " <if test='schoolName!=null '>"+
            " and s.name like  CONCAT(CONCAT('%', #{schoolName}), '%') "+
            " </if>"+
            "<if test='mobile!=null '>"+
            " and u.mobile like  CONCAT(CONCAT('%', #{mobile}), '%')"+
            " </if>"+
            " <if test='start!=null '>"+
            " and u.ctime &gt;=  #{start} "+
            " </if>"+
            " <if test='end!=null '>"+
            " and u.ctime &lt;=  #{end} "+
            " </if> "+
            " </script>")
    List<UserInfoDto> selectUserPage(Page<UserInfoDto> page,@Param("name") String name,@Param("mobile")String mobile,@Param("schoolName") String schoolName, @Param("start")Long  start, @Param("end")Long end);

     @Select("select u.* ,s.name as schoolName, a.* ,(select count(*) from book where u.id=user_id and status=10 ) as upBooks, (select count(*) from book where u.id=user_id ) as allBooks  from  user u " +
             " left join account a on u.id= a.user_id " +
             " left join school s on s.id=u.school_id  " +
             "  where u.id=#{userId} ")
     UserInfoDto getUserInfo(@Param("userId")Integer userId);
}
